﻿using IOP.Extension.DependencyInjection;
using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.Input;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;

namespace VkSample117;

public class TransformSystem : ComponentSystem
{
    private RenderObject Boom;
    [Autowired]
    private IGraphicsManager GraphicsManager;
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="contextManager"></param>
    public TransformSystem()
    {
    }

    public override void Initialization()
    {
        base.Initialization();
        GraphicsManager.TryGetRenderObject<RenderObject>("ZD", out Boom);
    }

    public override void Update(TimeSpan timeSpan)
    {
        ContextManager.Foreach<OBJGroup>((group) =>
        {
            Context context = ContextManager.GetContext(group.Entity);
            ref MVPMatrix mvp = ref context.GetMVPMatrix();
            var render = context.GetContextRenderObject();
            Transform transform = render.GetTransform();
            mvp.ModelMatrix = Matrix4x4.CreateScale(0.4f) * Matrix4x4.CreateTranslation(transform.Position);
            var a = render.GetComponents<ExpandAnimation>().First();
            a.Play(timeSpan);
            context.PushToRenderGroup();
            if(a.uFatFactor >= 20)
            {
                ContextManager.RemoveContext(group.Entity);
                ContextManager.CreateContexts(1, context.Archetype, context.GetContextRenderGroup(), Boom, true);
            }
        });
    }
}

public struct OBJGroup
{
    public Entity Entity;
    public TransformComponent Transform;
}
